#pragma strict
////////////////////////////////////////////////////////////////////////////////////
///fps摄影机.角色的眼睛部位.
////////////////////////////////////////////////////////////////////////////////////

var intensityX:float = 5;
var intensityY:float = 5;

var minimumX:float = -360;
var maximumX:float = 360;

var minimumY:float = -80;
var maximumY:float = 80;

var rotationX:float;
var rotationY:float;

var target:Transform;

private var originalRotation:Quaternion;

function Start () {
	originalRotation = transform.localRotation;
}

function Update () {
	rotationX += Input.GetAxis("Mouse X") * intensityX;
	rotationY += Input.GetAxis("Mouse Y") * intensityY;
	
	rotationX = ClampAngle(rotationX,minimumX,maximumX);
	rotationY = ClampAngle(rotationY,minimumY,maximumY);
	
	var qx:Quaternion = Quaternion.AngleAxis(rotationX,Vector3.up);
	var qy:Quaternion = Quaternion.AngleAxis(rotationY,Vector3.left);
	
	transform.localRotation = originalRotation * qx * qy;
	
	Follow();
}

function ClampAngle(angle:float,minimum:float ,maximum:float){
	if(angle > 360)
		angle -= 360;
	if(angle < -360)
		angle += 360;
	return Mathf.Clamp(angle,minimum,maximum);
}

function Follow(){
	if(target){
		transform.position = target.position;
		target.root.localEulerAngles.y = transform.localEulerAngles.y;
	}
}